<?php 

namespace app\admin\event;

use app\common\enum\BaseEnum;
use app\common\model\Suppliers as SuppliersModel;
use app\common\utils\JsonService;

class SuppliersEvent{

    /*
    * 供应商列表
    */
    public static function suppliersList(){
        $params = array_filter(request()->param());
        $page = ! empty($params['page']) && $params['page'] >= 1 ? $params['page'] : 1;
        $where = [];
        if($suppliers_id = isset($params['suppliers_id'])){
                    $where[] = [['suppliers_id','=',$params['suppliers_id']]];
            }
            if($suppliers_name = isset($params['suppliers_name'])){
                    $where[] = [['suppliers_name','like',"%{$params['suppliers_name']}%"]];
            }
            if($suppliers_contacts = isset($params['suppliers_contacts'])){
                    $where[] = [['suppliers_contacts','like',"%{$params['suppliers_contacts']}%"]];
            }
             $condition = [
            'where' => $where,
            'order' => ['suppliers_id' => 'desc'],
                'paginate' => [
                'page' => $page,
                'query' => $params,
            ],
        ];
        $suppliers = SuppliersModel::getPageAll($condition)->each(function($item){
            $item['is_check'] = BaseEnum::getBoolValue($item['is_check']);
        });
        return $suppliers;
    }


    /*
    * 新增供应商
    */
    public static function createSuppliers($data){

        try {
            SuppliersModel::create($data);
        } catch (\Exception $ex) {
            return JsonService::error($ex->getMessage());
        }
        return JsonService::success();
    }

    /*
    * 更新供应商
    */
    public static function updateSuppliers($data){

        try {
            SuppliersModel::updateOne($data, ['suppliers_id' => $data['suppliers_id']]);
        } catch (\Exception $ex) {
            return JsonService::error($ex->getMessage());
        }
        return JsonService::success();
    }


}

